<template>
  <div class="container">
    <!-- 导航栏 -->
    <van-nav-bar><template slot="title">
        <van-button
          round
          icon="search"
          to="/search"
        >
          搜索
        </van-button>
      </template></van-nav-bar>
    <!-- 文章频道列表 -->
    <van-tabs v-model="active">
      <van-tab
        :key="index"
        v-for="(item,index) in channels"
        :title="item.name"
      >

        <article-list :channel="item">
        </article-list>
      </van-tab>
      <template slot="nav-right">
        <div class="placeholder">
        </div>
      </template>
      <template slot="nav-right">
        <div
          class="hamburger-btn"
          @click="editShow=true"
        >
          <i class="iconfont icon-gengduo"> </i>
        </div>
      </template>
    </van-tabs>
    <!-- 编辑弹出层 -->
    <van-popup
      round
      close-icon-position="top-left"
      closeable
      v-model="editShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <edit-channels
        @active="activeChange"
        :myChannels="channels"
        :active="active"
      ></edit-channels>
    </van-popup>
  </div>
</template>
<script>
import { getUserChannels } from '../../api/article.js'
import ArticleList from './componets/article-list.vue'
import editChannels from './componets/edit-channels.vue'
import { getItem } from '@/utils/storage.js'
export default {
  data () {
    return {
      active: 0,
      channels: [],
      editShow: false
    }
  },
  created () {
    this.getUserChannels()
  },
  methods: {
    async  getUserChannels () {
      if (this.$store.state.user) {
        try {
          const { data: res } = await getUserChannels()
          console.log(res)
          this.channels = res.data.channels
        } catch (err) {
          this.$toast('获取频道列表失败')
        }
      } else if (getItem('myChannels')) {
        this.channels = getItem('myChannels')
      } else {
        try {
          const { data: res } = await getUserChannels()
          console.log(res)
          this.channels = res.data.channels
        } catch (err) {
          this.$toast('获取频道列表失败')
        }
      }
    },
    activeChange (flag, i) {
      if (flag) {
        this.active -= 1
      } else {
        this.editShow = false
        this.active = i
      }
    }
  },
  components: {
    ArticleList,
    editChannels
  }
}
</script>
<style lang="less" scoped>
/deep/ .van-nav-bar {
  background: #3296fa;
  .van-nav-bar__title {
    max-width: unset;
    color: #fff !important;
  }
  i {
    color: #ffffff;
  }
  .van-button {
    color: #ffffff;
    border: none;
    background: #5babfb;
    height: 64px;
    width: 555px;
  }
}

/deep/ .van-tabs {
  .van-tabs__nav {
    padding: 0 !important;
    .van-tab {
      min-width: 200px;
      border: 1px solid #edeff3;
      span {
        font-size: 30px;
      }
    }
    .van-tabs__line {
      bottom: 14px;
      width: 32px;
      height: 6px;
      background: #3296fa;
    }
  }
}
.hamburger-btn {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 88px;
  position: fixed;
  right: 0;
  width: 66px;
  background-color: rgb(255, 255, 255);
  opacity: 0.902;
  i {
    font-size: 33px;
  }
  &:before {
    content: "";
    position: absolute;
    left: 0;
    width: 1px;
    height: 100%;
    background-image: url(~@/assets/gradient-gray-line.png);
    background-size: contain;
  }
}
.placeholder {
  flex-shrink: 0;
  height: 72px;
  width: 66px;
}
</style>
